﻿Imports System.Drawing.Drawing2D

Class Renderer
	Inherits ToolStripSystemRenderer

	Dim Col1 As Color = Color.FromArgb(255, 192, 0)

	Private Sub DrawItem(e As ToolStripItemRenderEventArgs)
		Dim R = e.Item.ContentRectangle
		e.Graphics.SmoothingMode = SmoothingMode.HighQuality
		e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality
		If TypeOf e.Item Is ToolStripMenuItem AndAlso e.Item.IsOnDropDown Then
			R.Width -= 1
		ElseIf TypeOf e.Item Is ToolStripMenuItem Then
			R.Inflate(-2, 0)
		End If
		If TypeOf e.Item Is ToolStripSplitButton Then
			Dim b As ToolStripSplitButton = e.Item
			Debug.Print(b.Pressed & b.ButtonPressed & b.DropDownButtonPressed)
		End If
		If e.Item.Pressed Then
			R.Width -= 1 : R.Height -= 1
			e.Graphics.PixelOffsetMode = PixelOffsetMode.None
			Dim R1 = R
			R1.Height *= 1.5
			e.Graphics.FillRoundRect(New LinearGradientBrush(R1, Col1, Color.White, LinearGradientMode.Vertical), R, 4)
			e.Graphics.DrawRoundRect(Col1.Pen, R, 4)
			e.Graphics.DrawRoundRect(Color.Black.Opacity(32).Pen, R, 4)
		ElseIf e.Item.Selected Then
			R.Width -= 1 : R.Height -= 1
			e.Graphics.PixelOffsetMode = PixelOffsetMode.None
			Dim R1 = R
			R1.Inflate(0, R1.Height / 2)
			e.Graphics.FillRoundRect(New LinearGradientBrush(R1, Col1, Color.White, LinearGradientMode.Vertical), R, 4)
			e.Graphics.DrawRoundRect(Col1.Pen, R, 4)
			R.Inflate(-1, -1)
			e.Graphics.DrawRoundRect(Color.White.Opacity(128).Pen, R, 3)
		End If
	End Sub

	Sub New()
		'RoundedEdges = False
	End Sub

	Protected Overrides Sub OnRenderItemImage(e As System.Windows.Forms.ToolStripItemImageRenderEventArgs)
		'MyBase.OnRenderItemImage(e)
		e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality
		e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic
		e.Graphics.DrawImage(e.Image, e.ImageRectangle)
	End Sub

	Protected Overrides Sub OnRenderItemText(e As System.Windows.Forms.ToolStripItemTextRenderEventArgs)
		If e.Item.Enabled Then e.TextColor = Color.Black
		MyBase.OnRenderItemText(e)
	End Sub

	Protected Overrides Sub OnRenderMenuItemBackground(e As System.Windows.Forms.ToolStripItemRenderEventArgs)
		DrawItem(e)
	End Sub

	Protected Overrides Sub OnRenderButtonBackground(e As System.Windows.Forms.ToolStripItemRenderEventArgs)
		DrawItem(e)
	End Sub

	Protected Overrides Sub OnRenderSplitButtonBackground(e As System.Windows.Forms.ToolStripItemRenderEventArgs)
		DrawItem(e)
		Dim R = CType(e.Item, ToolStripSplitButton).DropDownButtonBounds
		OnRenderArrow(New ToolStripArrowRenderEventArgs(e.Graphics, e.Item, R, Color.Black, ArrowDirection.Down))
		If Not (e.Item.Selected Or e.Item.Pressed) Then Exit Sub
		e.Graphics.SmoothingMode = SmoothingMode.None
		e.Graphics.DrawLine(Col1.Pen, R.Left, R.Top, R.Left, R.Bottom)
	End Sub

	Protected Overrides Sub Initialize(toolStrip As System.Windows.Forms.ToolStrip)
		MyBase.Initialize(toolStrip)
		'If TypeOf toolStrip Is ToolStripDropDown Then
		toolStrip.Padding = New Padding(20) '.Empty
		toolStrip.Margin = New Padding(20)
		'End If
	End Sub

	Protected Overrides Sub InitializeItem(item As System.Windows.Forms.ToolStripItem)
		MyBase.InitializeItem(item)
		If TypeOf item Is ToolStripMenuItem AndAlso Not item.IsOnDropDown Then
			item.Padding = New Padding(4, 2, 4, 2)
			item.Margin = Padding.Empty
		ElseIf TypeOf item Is ToolStripMenuItem AndAlso item.IsOnDropDown Then
			item.Padding = New Padding(0, 2, 0, 1)
			'item.Padding = Padding.Empty
			item.Margin = New Padding(1, 1, 1, 0)
			'Else
			'	'item.Padding = New Padding(2)
			Dim m As ToolStripMenuItem = item
			m.Owner.Padding = New Padding(20)
			m.Owner.Margin = New Padding(20)
		Else
			item.Padding = New Padding(2)
		End If
	End Sub

	Protected Overrides Sub OnRenderToolStripBackground(e As System.Windows.Forms.ToolStripRenderEventArgs)
		'MyBase.OnRenderToolStripBackground(e)
		Dim R = e.AffectedBounds
		e.Graphics.FillRectangle(MainColor.Brush, R)
	End Sub

	Protected Overrides Sub OnRenderToolStripBorder(e As System.Windows.Forms.ToolStripRenderEventArgs)
		Dim R = e.ToolStrip.ClientRectangle
		If TypeOf e.ToolStrip Is ToolStripDropDown Then
			R.Width -= 1 : R.Height -= 1
			e.Graphics.DrawRectangle(Color.Black.Opacity(64).Pen, R)
		Else
			R.Height -= 1
			e.Graphics.DrawLine(Color.White.Opacity(16).Pen, R.Left, R.Top, R.Right, R.Top)
			e.Graphics.DrawLine(Color.Black.Opacity(16).Pen, R.Left, R.Bottom, R.Right, R.Bottom)
		End If
	End Sub

	Protected Overrides Sub OnRenderImageMargin(e As System.Windows.Forms.ToolStripRenderEventArgs)
		Dim R = e.AffectedBounds
		e.Graphics.FillRectangle(Color.White.Opacity(32).Brush, R)
	End Sub

	Protected Overrides Sub OnRenderSeparator(e As System.Windows.Forms.ToolStripSeparatorRenderEventArgs)
		'MyBase.OnRenderSeparator(e)
		e.Graphics.SmoothingMode = SmoothingMode.None
		Dim R = e.Item.Bounds
		R.Offset(-R.X, -R.Y)
		If e.Vertical Then
			Dim x As Integer = R.Left + R.Width / 2
			e.Graphics.DrawLine(Color.Black.Opacity(16).Pen, x, R.Top, x, R.Bottom)
			e.Graphics.DrawLine(Color.White.Opacity(16).Pen, x + 1, R.Top, x + 1, R.Bottom)
		Else
			Dim y As Integer = R.Top + R.Height / 2
			e.Graphics.DrawLine(Color.Black.Opacity(16).Pen, R.Left, y, R.Right, y)
			e.Graphics.DrawLine(Color.White.Opacity(16).Pen, R.Left, y + 1, R.Right, y + 1)
		End If
	End Sub
End Class
